#include "LinkTree.h"
#include <iostream>

TreeNode::TreeNode(const ElementType &element):data(element)
{
    parent = FirstChild = nextSbling = nullptr;
}

void TreeNode::Connect(TreeNode *parent)
{
    if(!parent)
        return;
    this->parent=parent;
    //头插
    this->nextSbling = parent->FirstChild;
    parent->FirstChild = this;  
}

void TreeNode::DisConnect(TreeNode *parent)
{
    if(parent == nullptr || this->parent != parent)
        return;
    

    //把我从双亲节点的孩子集合中删除
    TreeNode *travelPoint = parent->FirstChild;
    //由于树没有头结点，所以第一个要单独判断
    if(this == parent->FirstChild)
    {
        parent->FirstChild = this->nextSbling;
    }
    else
    {
        while(travelPoint->nextSbling != nullptr)
        {
            if(travelPoint->nextSbling == this)
            {
                travelPoint->nextSbling = this->nextSbling;
                break;
            }
            travelPoint = travelPoint->nextSbling; 
        }
    }
    //将自己的双亲和兄弟置为空
    parent = nextSbling = nullptr;
}

void TreeNode::FreeNode(TreeNode *node)
{
    //终止条件
    if(node == nullptr)
        return;
    FreeNode(node->FirstChild);
    FreeNode(node->nextSbling);
    delete node; // 最后释放自己
}

int TreeNode::GetNodeHeight(TreeNode *node)
{
    if(node == nullptr)
        return 0;
    
    int  MaxHeight = 0;
    //找最高的子树
    TreeNode* travelPoint = node->FirstChild;
    while(travelPoint != nullptr)
    {
        int height = GetNodeHeight(travelPoint);
        MaxHeight = MaxHeight > height ? MaxHeight : height;
        travelPoint = travelPoint->nextSbling;
    }
    //自身高度 = 最高子树高度 + 1
    return MaxHeight + 1;

}

void TreeNode::show(TreeNode *node, int depth)
{
    //终止条件
    if(node == nullptr)
        return;
    for(int i = 0;i<depth;i++)
        std::cout<<" ";
    //排除根节点打印
    if(depth > 0)
        std::cout<<"|_";
    if(node->parent != nullptr)
        std::cout<<node->data<<std::endl;
    //深度优先遍历
    show(node->FirstChild,depth+1);//递归
    show(node->nextSbling,depth);

    //广度优先遍历
    // show(node->nextSbling);
    // show(node->FirstChild);
}

TreeNode *TreeNode::GetParent()
{
    return parent;
}
std::string &TreeNode::GetData()
{
    return data;
}

void TreeNode::SetData(const ElementType &element)
{
    data = element;
}

LinkTree::LinkTree()
{
    root = new TreeNode("");
}

LinkTree::LinkTree(const ElementType &element)
{
    root = new TreeNode(element);
}

LinkTree::~LinkTree()
{
    TreeNode::FreeNode(root);
}

TreeNode *LinkTree::GetRoot()
{
    return root;
}

void LinkTree::show()
{
    root->show(root,-1);
}


TreeNode* TreeNode::FindNode(TreeNode* node,const ElementType& element)
{
    //终止条件
    if(node == nullptr )
        return nullptr;
    
    //排除根节点
    if(node->data == element && node->parent != nullptr)
        return node;
    TreeNode* target = FindNode(node->FirstChild,element);
    if(target)
        return target;
    target = FindNode(node->nextSbling,element);
    if(target)
        return target;
    
    return nullptr;
}

void TreeNode::FindNode(TreeNode *node, const ElementType &element, DARRAY::DynamicArray &a)
{
        //终止条件
    if(node == nullptr )
        return ;
    
    //排除根节点
    if(node->data == element && node->parent != nullptr)
    {
        a.InsertTail(node);
    }
    FindNode(node->FirstChild,element,a);
    FindNode(node->nextSbling,element,a);
}

DARRAY::DynamicArray LinkTree::FindByElement(const ElementType &element)
{
    DARRAY::DynamicArray a;
    TreeNode::FindNode(root,element,a);
    return a;
}

int LinkTree::GetTreeHeight()
{
    //树根那层不算
    return TreeNode::GetNodeHeight(root) - 1;
}

std::string LinkTree::GetNodePath(TreeNode *node)
{
    if(node == nullptr)
        return std::string();

    std::string path = node->GetData();
    while(node->GetParent() != nullptr)
    {
        path = node->GetParent()->GetData() + "/" + path;
        node = node->GetParent();
    }
    return path;
}
